package test.controller;

import com.alibaba.fastjson2.JSON;
import com.alibaba.fastjson2.JSONObject;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.ResponseBody;
import test.utils.ObjectSerializableUtil;

import java.io.*;
import java.util.Map;

/**
 * Created with IntelliJ IDEA.
 *
 * @ project name : example
 * @ Author: XuLeHuang
 * @ Date: 2022/7/26 9:54
 * @ Description:
 */
@Controller
public class FileHandelController {
    @PostMapping("/boc/StringToJsonTrans")
    @ResponseBody
    public String StringToJsonTrans(@RequestBody String str) {
        //获取String字符串
        //转成json
        JSONObject jsonObject = JSON.parseObject(str);
        //获取文件标识符
        Map head = (Map)jsonObject.get("reqHeader");
        String fileFlag = (String) head.get("serviceId");
        //根据文件标识符获取文件内容
        String suffix = ".txt";
        String respString = "";
        try ( FileInputStream file = new FileInputStream("D:\\资料\\神州信息\\" + fileFlag + suffix);
              InputStreamReader reader = new InputStreamReader(file,"UTF-8");
              BufferedReader br = new BufferedReader(reader);)  {
            String line;
            while ((line = br.readLine()) != null) {
                System.out.println(line);
                respString += line;
            }
        } catch (FileNotFoundException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        }
        //将文件内容转成json返回
        return respString;
    }

    @PostMapping("/boc/StringToJsonTransV2")
    @ResponseBody
    public String StringToJsonTransV2(@RequestBody String str) {
        //获取String字符串
        //转成json
        JSONObject jsonObject = JSON.parseObject(str);
        //获取文件标识符
        Map head = (Map)jsonObject.get("reqHeader");
        String fileFlag = (String) head.get("serviceId");
        //根据文件标识符获取文件内容
        String suffix = ".json";
        String respString = "";
        try ( FileInputStream file = new FileInputStream("D:\\资料\\神州信息\\" + fileFlag + suffix);
              InputStreamReader reader = new InputStreamReader(file,"UTF-8");
              BufferedReader br = new BufferedReader(reader);)  {
            String line;
            while ((line = br.readLine()) != null) {
                System.out.println(line);
                respString += line;
            }
        } catch (FileNotFoundException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        }
        //将文件内容转成json返回
        return respString;
    }

    @PostMapping("/boc/StringToJsonTransV3")
    @ResponseBody
    public String StringToJsonTransV3(@RequestBody String str) {
        //获取String字符串
        //转成json
        JSONObject jsonObject = JSON.parseObject(str);
        //获取文件标识符
        Map head = (Map)jsonObject.get("reqHeader");
        String fileFlag = (String) head.get("serviceId");
        //根据文件标识符获取文件内容
        String suffix = ".txt";
        String respString = "";
        try ( FileInputStream file = new FileInputStream("D:\\资料\\神州信息\\" + fileFlag + suffix);
              ObjectInputStream  objectInputStream = new ObjectInputStream (file);)  {
            Map map = (Map) objectInputStream.readObject();
            System.out.println(map);

        } catch (FileNotFoundException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        } catch (ClassNotFoundException e) {
            e.printStackTrace();
        }
        //将文件内容转成json返回
        return respString;
    }

    @PostMapping("/boc/StringToJsonTransV4")
    @ResponseBody
    public String StringToJsonTransV4(@RequestBody String str) {
        //获取String字符串
        //转成json
        JSONObject jsonObject = JSON.parseObject(str);
        //获取文件标识符
        Map head = (Map)jsonObject.get("reqHeader");
        String fileFlag = (String) head.get("serviceId");
        //根据文件标识符获取文件内容
        String suffix = ".txt";
        String respString = "";
        String respStringV2 = "";
        try ( FileInputStream file = new FileInputStream("D:\\资料\\神州信息\\" + fileFlag + suffix);
              InputStreamReader reader = new InputStreamReader(file,"UTF-8");
              BufferedReader br = new BufferedReader(reader);)  {
            String line;
            while ((line = br.readLine()) != null) {
                System.out.println(line);
                respString += line;
            }
            //序列化和反序列化要采用同一个流，反则仅仅从复制粘贴到文本的内容反序列化会出错：invalid stream header: 7B0D0A09
            ObjectSerializableUtil.doWriteObjectBySerializable(respString);
            respStringV2 = ObjectSerializableUtil.doGetObjectFromRejectSerializable("D:\\资料\\神州信息\\" + "smpv.ts0008" + suffix);
            System.out.println("反序列化："+respStringV2);

        } catch (FileNotFoundException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        }
        //将文件内容转成json返回
        return respStringV2;
    }
}
